<template>
  <div>

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item label="问卷名称" :labelCol="{span: 5}" :wrapperCol="{span: 18, offset: 1}">
              <a-input placeholder="请输入名称查询" v-model="queryParam.questionTopic" allow-clear></a-input>
            </a-form-item>
          </a-col>

          <a-col :md="10" :sm="12">
            <a-form-item label="创建时间" :labelCol="{span: 5}" :wrapperCol="{span: 18, offset: 1}">
              <a-range-picker v-model="dateTimeRange" :show-time="{
                hideDisabledOptions: true,
                defaultValue: [moment('00:00:00', 'HH:mm:ss'), moment('11:59:59', 'HH:mm:ss')],
              }" format="YYYY-MM-DD HH:mm:ss" show-time @change="onChangeDateRange" allow-clear />
            </a-form-item>
          </a-col>

          <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
            <a-col :md="6" :sm="24">
              <a-button type="primary" @click="searchQuery">查询</a-button>
              <a-button style="margin-left: 8px" @click="searchReset">重置</a-button>
            </a-col>
          </span>

        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator" style="margin-top: 5px">
      <a-button @click="modelFormHandle" type="primary" icon="plus">新建问卷</a-button>
    </div>

    <a-tabs v-model="queryParam.questionStatus" @change="onChangeStatus">
      <a-tab-pane key="" tab="全部"></a-tab-pane>
      <a-tab-pane :key="0" tab="未发布"></a-tab-pane>
      <a-tab-pane :key="1" tab="已发布"></a-tab-pane>
    </a-tabs>

    <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="questionId"
        :columns="columns"
        :dataSource="dataList"
        :pagination="ipagination"
        :loading="loading"
        @change="tableChangeHandle">

      <template slot="questionStatus" slot-scope="text, record">
        <a-switch :checked="text === 1" checked-children="是" un-checked-children="否"
                  @change="(value) => changeStatus(record, value)"></a-switch>
      </template>

        <span slot="action" slot-scope="text, record">
          <a @click="gotoRouter(record.questionId, '基本信息')">编辑</a>
          <a-divider type="vertical" />


          <a-dropdown>
            <a class="ant-dropdown-link">
              更多 <a-icon type="down" />
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="gotoRouter(record.questionId, '题目管理')">题目管理</a>
              </a-menu-item>
              <a-menu-item>
                <a @click="gotoRouter(record.questionId, '评估关系')">评估关系</a>
              </a-menu-item>
              <a-menu-item>
                <a @click="gotoRouter(record.questionId, '问卷分析')">问卷分析</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => deleteHandle(record.questionId)" okText="确定" cancelText="取消">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>


    </a-table>

  </div>
</template>

<script>
import listMixin from '@/mixins/list-mixin'
import moment from 'moment';
import { putAction } from '@/api/manage'

export default {
  name: "list",
  mixins: [listMixin],
  data () {
    return {
      disableMixinActivated: true,
      queryParam: {
        questionStatus: ''
      },
      dateTimeRange: [],
      dataList: [],
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          }
        },
        {
          title: '问卷名称',
          align: "center",
          dataIndex: 'questionTopic'
        },
        {
          title: '问卷类型',
          align: "center",
          dataIndex: 'questionType',
          customRender: function (text) {
            if (text === 1) {
              return '个人问卷'
            }
          }
        },
        {
          title: '问卷数量',
          align: "center",
          dataIndex: 'questionCount'
        },
        {
          title: '创建人',
          dataIndex: 'questionCreateName',
          align: "center"
        },
        {
          title: '创建时间',
          dataIndex: 'questionCreateTime',
          align: "center"
        },
        {
          title: '截止时间',
          dataIndex: 'questionEndTime',
          align: "center"
        },
        {
          title: '发布状态',
          dataIndex: 'questionStatus',
          align: "center",
          scopedSlots: { customRender: 'questionStatus' },
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          width: 150,
          fixed: 'right',
          scopedSlots: { customRender: 'action' },
        }
      ],
      url: {
        list: '/question/getQuestionList',
        delete: '/question/deleteQuestion',
        update: '/question/updateQuestionList',
      }
    }
  },
  methods: {
    moment,
    onChangeDateRange (dates, dateStrings) {
      console.log('dates', dates)
      console.log('dateStrings', dateStrings)
      this.queryParam.questionStartTime = dateStrings[0]
      this.queryParam.questionEndTime = dateStrings[1]
    },
    searchReset() {
      this.dateTimeRange = []
      this.queryParam = this.$options.data().queryParam
      this.loadData();
    },
    changeStatus(record, value) {
//       开关校验
//       1.题目是否已设置
//       2.评估关系是否已设置
//       不满足其中一任意一项则给出相应提示
//        “您还未设置题目/评估关系，确认发布吗？”
//        点击确认可发布
      if (value) {
        let num = record.questionCount.split('/')[1]
        if (record.titleCount === 0) {
          let that = this;
          this.$confirm({
            title: "确认发布",
            content: "您还未设置题目管理，确认发布吗?",
            onOk: function () {
              that.updateStatus(record, value)
            }
          })
        } else if (num === '0') {
          let that = this;
          this.$confirm({
            title: "确认发布",
            content: "您还未设置评估关系，确认发布吗?",
            onOk: function () {
              that.updateStatus(record, value)
            }
          })
        } else {
          this.updateStatus(record, value)
        }
      } else {
        this.updateStatus(record, value)
      }
    },
    updateStatus(record, value) {
      this.loading = true
      let status = value ? 1 : 0
      putAction(this.url.update, { questionId: record.questionId, questionStatus: status }).then(res => {
        this.loading = false
        if (res.success) {
          record.questionStatus = status
          this.$message.success(res.message)
        } else {
          this.$message.error(res.message)
        }
      }).catch(() => {
        this.loading = false
      })
    },
    onChangeStatus () {
      this.searchQuery()
    },
    modelFormHandle () {
      this.$router.push({ name: 'questionnaire-form-modal' })
    },
    gotoRouter (id, titleKey) {
      this.$router.push({ name: 'questionnaire-basic', query: { questionId: id, titleKey: titleKey } })
    }
  }
}
</script>

<style scoped>

</style>
